<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Models\Relations\CommonTrait;

class Courses extends Model
{
    use CommonTrait;
    protected $table="courses";
    
    protected $dates = ['deleted_at'];

    protected $fillable = ['id','name','org_id','cost', 'score', 'effect', 'teachers','environment','breif', 'introduction', 'activities', 'is_recommend'];

    /*public function attachments() {
        return $this->hasMany('App\Models\Attachment', '')
    }*/

    public function scores() {
        return $this->hasMany('App\Models\CourseScore', 'c_id','id');
    }
    
    /**
     * 一对一关联 附件信息
     */
    public function attachment()
    {
        return $this->hasOne('App\Models\Attachment','aid','thumb');
    }
    
    /**
     * 获取机构信息
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function organization()
    {
        return $this->hasOne('App\Models\Organization','id','org_id');
    }
    
    /** 
     * 通过机构关联取得机构分类的信息
     * @return type
     */
//    public function categorys()
//    {
//        return $this->hasManyThrough('App\Models\Category', 'App\Models\Organization','id','cat_id');
//    }
    
    public function orcoursesd($id){
        return DB::table('attachment')->where('aid','=', $id)->get();
    }
    /***
     * 获取课程图片和课程信息
     */
    public function getListShow($org_ids,$skip,$page_size)
    {
      $query = DB::table($this->table)
              ->leftJoin('attachment','courses.thumb','=','attachment.aid')
              ->leftJoin('organization','courses.org_id','=','organization.id')
              ->leftJoin('category','category.cat_id','=','organization.cat_id')
              ->select('courses.*','attachment.url','category.cat_name','category.cat_id')
              ->whereIn('courses.org_id',$org_ids)
              ->skip($skip)
              ->take($page_size)
              ->get();
        return $query;
 
    }
}
